package com.jy.rhin.facade.cda;

import com.jy.rhin.application.cda.CdaApplication;
import com.jy.rhin.sdk.dto.document.query.DocumentValidateResultDetailDto;
import com.jy.rhin.sdk.dto.document.query.DocumentValidateResultDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author WeiQ
 * @date 2023/3/27 10:36
 */
@Slf4j
@RequestMapping("/test")
@Api(tags = "测试cda验证功能", hidden = true)
@RestController
public class TestValidController {

    @Autowired
    private CdaApplication cdaApplication;

    @ApiOperation("验证")
    @GetMapping("/valid")
    public String valid(String name) throws Exception {
        String result = "";
        String doc = getDoc(name);
        log.info("解析文档:{}", doc);
        DocumentValidateResultDto validate = cdaApplication.validate(doc);
        result = "验证结果:\n" + "成功数: "+ validate.getSuccessNum() + "失败数: " + validate.getFailNum() +"\n";
        for (DocumentValidateResultDetailDto detail : validate.getDetails()){
            result += "校验结果: " + detail.getDetailResult() +"   校验结果描述: "+ detail.getContent() + "\n";
        }
        return result;
    }

    String getDoc(String name) {
        try {
            return IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("cda/example/" + name));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
